/** 地图API
 * Author:      cn2008tv 
 * CreateTime:  2023/2/5 18:45:24 
 * LastEditor:  cn2008tv 
 * ModifyTime:  2023/12/5 20:21:03 
 * Description: 
*/ 
import Map from "ol/Map";
import TileLayer from "ol/layer/Tile";
import XYZ from "ol/source/XYZ";
import View from "ol/View";
import { fromLonLat, transform, transformExtent } from "ol/proj";
import proj4 from "proj4";
import Overlay from "ol/Overlay";
import { Image as ImageLayer, Vector as VectorLayer } from "ol/layer";
import ImageStatic from "ol/source/ImageStatic";
import MousePosition from "ol/control/MousePosition";
import { createStringXY } from "ol/coordinate";
import Polygon from "ol/geom/Polygon";
import { Style, Fill, Stroke, Text, Icon } from "ol/style";
import Feature from "ol/Feature";
import Point from "ol/geom/Point";
import VectorSource from "ol/source/Vector";
import Draw from "ol/interaction/Draw";
import { createBox } from "ol/interaction/Draw";
import { ref } from "vue";


export const mapEnv = {
  baseMapUrl: "https://map.geoq.cn/ArcGIS/rest/services/ChinaOnlineStreetPurplishBlue/MapServer/tile/{z}/{y}/{x}",
  epsg4326: "EPSG:4326",
  epsg3857: "EPSG:3857",
  centerXy: [124.0267, 36.8813],
  zoom: 8,
  controlLayerServiceId: 11,
  mHomeDrawingId: 149,
  baseWellOffset: [0, 0],
  baseAniWellOffset: [20, -8],
  baseAniWellOffsetIndex: [-21, -18],
  x_0Parameter: 1000000,
  aniDuration: 2000,
  aniZoom: 13,
  bbox: [7077701.75725138, 1266962.43286423, 17096455.928646, 6946539.38256596],
  bsize: [2048, 1161],
  mapCenter: [123.94522, 47.94366],
  extent: [12744056.92809892, 7092191.67316922, 15048474.40142107, 5216819.07061724],
  deZoom: 4.5,
  onLineType: 1,
  duration: 1000,
  pointTypes: "icon",
  sxScale: 1,
  zjScale: 0.6,
  cqScale: 0.28,
  kcScale: 0.6,
  dlScale: 0.38,
  sxFontSize: "30px sans-serif",
  zjCountsFontSize: "14px sans-serif",
  imageIconcrossOrigin: "anonymous",
  imageIconanchor: [0.5, 0.96],
  bottomCenter: "bottom-center",
  bottomLeft: "bottom-left",
  offsetLeft: [0, -42],
  offsetRight: [85, -3],
  offsetY: 65,
  kcOffsetY: 75,
  zjOffsetY: 28,
  cqkOffsetY: 60,
  fileColor: "white",
  sxstrokeColor: "black",
  zjstrokeColor: "black",
  strokeWidth: 10,
  zoomr: "change:resolution",
  left: "click",
  right: "contextmenu",
  mOn: "pointermove",
  minZoom: 1,
  maxZoom: 19,
  nAksuColor: "rgba(93,144,220,0.4)",
  undefined: "undefined",
  coordinateFormats: 8,
  mosuePosition: "mosuePosition",
  undefinedHTML: "&nbsp",
  moveend: "moveend",
  myPosition: "myPosition",
  drawend: "drawend",
  Polygon: "Polygon",
  Circle: "Circle",
  LineString: "LineString",
  extentArrs: [71.98242, 53.69671, 135.13184, 17.81146],
  drawAnArea: [{ id: "circular", label: "绘制圆形选井" }, {
    id: "polygon",
    label: "绘制不规则多边形选井"
  }, { id: "brokenLine", label: "绘制折线选井" }, { id: "rectangular", label: "绘制矩形选井" }]
};

export const baseMap = (target) => {
  let map = new Map({
    layers: [
      new TileLayer({
        source: new XYZ({
          url: mapEnv.baseMapUrl,
          //url: '',
          projection: mapEnv.epsg3857
        })
      })
    ],
    target: target,
    view: new View({
      center: transform(mapEnv.centerXy, mapEnv.epsg4326, mapEnv.epsg3857),
      zoom: mapEnv.zoom
    })
  });
  return map;
};

export function brokenLine(mapGlobal) {
  let wellArrChosed = [];
  let drawLayer = new VectorLayer({
    source: new VectorSource()
  });
  mapGlobal.addLayer(drawLayer);
  let draw = new Draw({
    source: drawLayer.getSource(),
    type: mapEnv.LineString
  });

  mapGlobal.addInteraction(draw);
  draw.on(mapEnv.drawend, function(event) {
    let vs = event.feature.getGeometry().getCoordinates();
    console.info(vs);
    mapGlobal.removeInteraction(draw);
    //mapGlobal.removeLayer(drawLayer);
  });
}

/*export function rectangular(mapGlobal) {
  let wellArr = [];
  let drawLayer = new VectorLayer({
    source: new VectorSource()
  });
  mapGlobal.addLayer(drawLayer);
  let draw = new Draw({
    source: drawLayer.getSource(),
    type: mapEnv.Circle,
    geometryFunction: createBox()
  });
  mapGlobal.addInteraction(draw);
  draw.on(mapEnv.drawend, function(event) {
    let wellDataArrs = that.peon;
    wellDataArrs.map(item => {
      if (ModuleExports(proj4(projDq(parseInt(item.HZB / mapEnv.x_0Parameter + "")), mapEnv.EPSG3857, [item.HZB, item.ZZB]), event.feature.getGeometry().getCoordinates()[0])) {
        wellArr.push(item);
      }
    });
    mapGlobal.removeInteraction(draw);
    mapGlobal.removeLayer(drawLayer);
    that.drawingCallBack(wellArr);
  });
}

export function polygon(mapGlobal) {
  let wellArr = [];
  let drawLayer = new VectorLayer({
    source: new VectorSource()
  });
  mapGlobal.addLayer(drawLayer);
  let draw = new Draw({
    source: drawLayer.getSource(),
    type: mapEnv.Polygon
  });
  mapGlobal.addInteraction(draw);
  draw.on(mapEnv.drawend, function(event) {
    let wellDataArrs = that.peon;
    wellDataArrs.map(item => {
      if (ModuleExports(proj4(projDq(parseInt(item.HZB / mapEnv.x_0Parameter + "")), mapEnv.EPSG3857, [item.HZB, item.ZZB]), event.feature.getGeometry().getCoordinates()[0])) {
        wellArr.push(item);
      }
    });
    mapGlobal.removeInteraction(draw);
    mapGlobal.removeLayer(drawLayer);
    that.drawingCallBack(wellArr);
  });
}*/
